<?php
namespace app\massage\model;

use app\BaseModel;
use app\restaurant\model\Table;
use think\facade\Db;

class Printer extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_printer';



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        if(empty($data)){

            $this->dataAdd($dis);

            $data = $this->where($dis)->find();

        }

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:订单打印
     */
    public function userOrder($id){

        $order_model   = new Order();
        //订单信息
        $order = $order_model->dataInfo(['id'=>$id]);

        $brs = "<BR>";

        $orderInfo = '<CB>'.'订单小票'.'</CB><BR>';

        $orderInfo .= '--------------------------------'.$brs;


        $orderInfo .= '服务/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;


        foreach ($order['order_goods'] as $v) {

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            $orderInfo .= $v['goods_name'].$brs;

            $orderInfo .= '  X'.$v['num'].'               '.round($v['true_price']*$v['num'],2).'元'.$brs;
//            $orderInfo .=  round($v['true_price']*$v['num'],2).'元'.$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }


        $coach_model = new Coach();

        $address_model = new OrderAddress();

        $coach_name  = $coach_model->where(['id'=>$order['coach_id']])->value('coach_name');

        $user_name   = $address_model->where(['order_id'=>$order['id']])->value('user_name');

        $orderInfo .= '预约技师:'.$coach_name.$brs;

        $orderInfo .= '下单人:'.$user_name.$brs;

        $time = date('Y-m-d H:i',$order['start_time']).'-'.date('Y-m-d H:i',$order['end_time']);

        $orderInfo .= '预约时间:'.$time.$brs;

        $orderInfo .= '预约金额:'.round($order['pay_price'],2).$brs;

        $orderInfo .= '付款时间:'.date('Y-m-d H:i',$order['pay_time']).$brs;

        return $orderInfo;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-11-11 10:20
     * @功能说明:打印
     */
    public function orderPrinter($orderInfo,$aotu=1,$type=1){

        $dis = [

            'status'   => 1,
        ];

        if($aotu==1){

            $dis['auto'] =1;
        }

        if($type==1){

            $dis['is_car'] = 1;

        }elseif($type==2){

            $dis['is_shop'] = 1;

        }else{

            $dis['is_restaurant'] = 1;

        }

        $printer_config = $this->where($dis)->select()->toArray();

        if(!empty($printer_config)){

            foreach ($printer_config as $value){

                $value['ukey'] = $value['api_key'];

                $value['sn']   = $value['printer_key'];
                //用户小票
                $res = \longbingcore\printer\Printer::FeiePrintMsg($value,$orderInfo,'Open_printMsg',$value['user_ticket_num']);

            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 09:57
     * @功能说明:打印
     *
     * type 1赛车 2商城 3 餐饮
     */
    public function printer($id,$aotu=1,$type=1){

        if($type==1){

            $user_order = $this->carOrder($id);

        }elseif ($type==2){

            $user_order = $this->shopOrder($id);

        }else{

            $user_order = $this->restaurantOrder($id);

        }
        //打印
        $res = $this->orderPrinter($user_order,$aotu,$type);

        return $res;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:商城订单打印
     */
    public function shopOrder($id){

        $order_model   = new \app\shop\model\Order();
        //订单信息
        $order = $order_model->dataInfo(['id'=>$id]);

        $brs = "<BR>";

        $orderInfo = '<CB>'.'商城订单小票'.'</CB><BR>';

        $orderInfo .= '订单号：'.$order['order_code'].$brs;

        $orderInfo .= '下单时间：'.date('Y-m-d H:i:s',$order['create_time']).$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/规格/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        foreach ($order['order_goods'] as $v) {

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            $orderInfo .= $v['goods_name'].$brs;

            $orderInfo .= '【'.$v['spe_name'].'】'.$brs;

            $orderInfo .= '  X'.$v['goods_num'].'               '.round($v['pay_price'],2).'元'.$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }
        $orderInfo .= '--------------------------------'.$brs;

        if($order['send_type']==1){

            $orderInfo .= '配送方式:自提'.$brs;

        }else{

            $orderInfo .= '配送方式:快递'.$brs;

            $orderInfo .= '物流费:'.$order['freight'].$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;
        }

        $orderInfo .= '原价:'.$order['init_price'].$brs;

        $orderInfo .= '优惠:'.round($order['init_price']-$order['pay_price'],2).$brs;

        $orderInfo .= '实付:'.$order['pay_price'].$brs;

        $address_model = new OrderAddress();

        $user          = $address_model->dataInfo(['order_id'=>$order['id'],'type'=>2]);
        //自提
        $orderInfo .= $user['address'].$user['address_info'].$brs;

        $orderInfo .= $user['user_name'].' '.$user['mobile'].$brs;

        $orderInfo .= '备注:'.$order['text'].$brs;

        return $orderInfo;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:餐饮订单打印
     */
    public function restaurantOrder($id){

        $order_model   = new \app\restaurant\model\Order();
        //订单信息
        $order = $order_model->dataInfo(['id'=>$id]);

        $table_model = new Table();

        $table_title = $table_model->where(['id'=>$order['table_id']])->value('title');

        $brs = "<BR>";

        $orderInfo = '<CB>'.'餐饮订单小票'.'</CB><BR>';

        $orderInfo .= '订单号：'.$order['order_code'].$brs;

        $orderInfo .= '人数：'.$order['user_num'].$brs;

        $orderInfo .= '桌号：'.$table_title.$brs;

        $orderInfo .= '下单时间：'.date('Y-m-d H:i:s',$order['create_time']).$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/规格/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        foreach ($order['order_goods'] as $v) {

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            $orderInfo .= $v['goods_name'].$brs;

            $orderInfo .= '【'.$v['spe_name'].'】'.$brs;

            $orderInfo .= '  X'.$v['goods_num'].'               '.round($v['pay_price'],2).'元'.$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }
        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '原价:'.$order['init_price'].$brs;

        $orderInfo .= '优惠:'.round($order['init_price']-$order['pay_price'],2).$brs;

        $orderInfo .= '实付:'.$order['pay_price'].$brs;

        $user_model = new User();

        $user       = $user_model->dataInfo(['id'=>$order['user_id']]);

        $orderInfo .= $user['nickName'].' '.$user['phone'].$brs;

        $orderInfo .= '备注:'.$order['text'].$brs;

        return $orderInfo;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-11 09:25
     * @功能说明:赛车订单打印
     */
    public function carOrder($id){

        $order_model   = new Order();
        //订单信息
        $order = $order_model->dataInfo(['id'=>$id]);

        $brs = "<BR>";

        $orderInfo = '<CB>'.'赛车订单小票'.'</CB><BR>';

        $orderInfo .= '订单号：'.$order['order_code'].$brs;

        $orderInfo .= '下单时间：'.date('Y-m-d H:i:s',$order['create_time']).$brs;

        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '商品/数量/价格'.$brs;

        $orderInfo .= '--------------------------------'.$brs;

        foreach ($order['order_goods'] as $v) {

            $v['goods_name'] = mb_convert_encoding($v['goods_name'], "UTF-8", "auto");

            $orderInfo .= $v['goods_name'].$brs;

            $orderInfo .= '  X'.$v['num'].'               '.round($v['pay_price'],2).'元'.$brs;

            $orderInfo .= ''.$brs;

            $orderInfo .= ''.$brs;

        }
        $orderInfo .= '--------------------------------'.$brs;

        $orderInfo .= '原价:'.$order['init_price'].$brs;

        $orderInfo .= '优惠:'.round($order['init_price']-$order['pay_price'],2).$brs;

        $orderInfo .= '实付:'.$order['pay_price'].$brs;

        $user_model = new User();

        $user       = $user_model->dataInfo(['id'=>$order['user_id']]);

        $orderInfo .= $user['nickName'].' '.$user['phone'].$brs;

        $orderInfo .= '备注:'.$order['text'].$brs;

        return $orderInfo;

    }






}